package util;
import java.io.File;
import java.net.URL;

import constant.Constant;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import util.tools.CommonTools;

/**
 * 封装客户端app启动的各种情况的driver
 * @auther Benchi
 * @create 2017/7/26  16:47
 */
public class InitDrivers {
    /**
     * 客户端初始化通用设置
     * @param uuid 设备标识 例如：模拟器为 127.0.0.1:62001
     * @return DesiredCapabilities 对象
     */
    public static DesiredCapabilities getCmmCaps(String uuid){
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability(MobileCapabilityType.DEVICE_NAME, "xx");
        caps.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 600);
        caps.setCapability(AndroidMobileCapabilityType.NO_SIGN, true);
        caps.setCapability(AndroidMobileCapabilityType.UNICODE_KEYBOARD, true);
        caps.setCapability(AndroidMobileCapabilityType.RESET_KEYBOARD, true);
        caps.setCapability(MobileCapabilityType.UDID, uuid);
        caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "cn.ishansong");
        caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, "cn.ishansong.SplashActivity");
        return caps;
    }
    /**
     * 闪送员端初始化通用设置
     * @param uuid 设备标识 例如：模拟器为 127.0.0.1:62001
     * @return DesiredCapabilities 对象
     */
    public static DesiredCapabilities getssyCmmCaps(String uuid){
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability(MobileCapabilityType.DEVICE_NAME, "xx");
        caps.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 600);
        caps.setCapability(AndroidMobileCapabilityType.NO_SIGN, true);
        caps.setCapability(AndroidMobileCapabilityType.UNICODE_KEYBOARD, true);
        caps.setCapability(AndroidMobileCapabilityType.RESET_KEYBOARD, true);
        caps.setCapability(MobileCapabilityType.UDID, uuid);
        caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.ishansong");
        caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, "com.ishansong.LoadingActivity");
        return caps;
    }
    /**
     * 以安装apk包的方式进行driver初始化
     * @param uuid 设备标识
     * @param apkPath 安装包路径
     * @return
     * @throws Exception
     */
    public static AndroidDriver<AndroidElement> getDriverNoInstall(String uuid, String apkPath)
            throws Exception{
        File appFile = new File(apkPath);
        DesiredCapabilities caps=getCmmCaps(uuid);
        caps.setCapability(MobileCapabilityType.APP, appFile);
        AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(
                new URL("http://127.0.0.1:4723/wd/hub"),caps);
        if(CommonTools.isElementExist(driver,
                By.id("com.huawei.systemmanager:id/btn_allow"))){
            System.out.println("华为手机管家有权限设置的提示弹出！");
            driver.findElement( By.id("com.huawei.systemmanager:id/btn_allow")).click();
        }
        return driver;
    }
    /**
     * 安装后的形式来初始化dirver
     * @param uuid 设备标识
     * @return
     * @throws Exception
     */
    public static AndroidDriver<AndroidElement> getDriverWithInstalled(String uuid)
            throws Exception{
        DesiredCapabilities caps=getCmmCaps(uuid);
        AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(
                new URL("http://127.0.0.1:4723/wd/hub"),caps);
//        System.out.println("===============init");
        return driver;
    }
    /**
     * 保持之前的登录状态diver
     * @param uuid
     * @return
     * @throws Exception
     */
    public static AndroidDriver<AndroidElement> getDriverWithLogined(String uuid)
            throws Exception{
        DesiredCapabilities caps=getCmmCaps(uuid);
        caps.setCapability(MobileCapabilityType.NO_RESET, true);
        AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(
                new URL("http://127.0.0.1:4723/wd/hub"),caps);
//				new URL("http://127.0.0.1:4491/wd/hub"),caps);
        return driver;
    }



    /**
     * 多设备启动
     * 以安装apk包的方式进行driver初始化
     * @param uuid 设备标识
     * @param apkPath 安装包路径
     * @return
     * @throws Exception
     */
    public static AndroidDriver<AndroidElement> getDriverNoInstall(String server,String apkPath,String uuid)
            throws Exception{
        File appFile = new File(apkPath);
        DesiredCapabilities caps=getCmmCaps(uuid);
        caps.setCapability(MobileCapabilityType.APP, appFile);
        AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(
                new URL("http://"+server+"/wd/hub"),caps);
        return driver;
    }

    /**
     * 多设备启动
     * 保持app已安装状态diver
     * @param uuid 设备标识
     * @return
     * @throws Exception
     */
    public static AndroidDriver<AndroidElement> getDriverWithInstalled(String server,String uuid)
            throws Exception{
        DesiredCapabilities caps=getCmmCaps(uuid);
        caps.setCapability(MobileCapabilityType.NO_RESET, true);
        AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(
                new URL("http://"+server+"/wd/hub"),caps);
        return driver;
    }

    /**
     * 多设备启动
     * 保持之前的登录状态diver
     * @param uuid 设备标识
     * @return
     * @throws Exception
     */
    public static AndroidDriver<AndroidElement> getDriverWithLogined(String server,String uuid)
            throws Exception{
        DesiredCapabilities caps=getCmmCaps(uuid);
        caps.setCapability(MobileCapabilityType.NO_RESET, true);
        AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(
                new URL("http://"+server+"/wd/hub"),caps);
        return driver;
    }

    /**
     * 闪送员端 保持之前的登录状态diver
     * @param uuid
     * @return
     * @throws Exception
     */
    public static AndroidDriver<AndroidElement> getssyDriverWithLogined(String server,String uuid)
            throws Exception{
        DesiredCapabilities caps=getssyCmmCaps(uuid);
        caps.setCapability(MobileCapabilityType.NO_RESET, true);
        AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(
                new URL("http://"+server+"/wd/hub"),caps);
//				new URL("http://127.0.0.1:4491/wd/hub"),caps);
        return driver;
    }
    }




